#include "singlecurvedraw.h"

#include <QValueAxis>
#include <qlineseries.h>

SingleCurveDraw::SingleCurveDraw( )
{

}

void SingleCurveDraw::draw(QChartView *chartView, QString titleName, QString lineName, const QVector<double> &xDataList, const QVector<double> &yDataList)
{
    // 定义曲线样式
    const QColor colors = QColor(127, 127, 255, 255);

    QChart* chart = new QChart();
    chart->setBackgroundBrush(Qt::transparent);
    chart->setMargins(QMargins(0, 0, 0, 0));
    chart->setTitle(titleName);
    chart->setAnimationOptions(QChart::SeriesAnimations);
    chart->legend()->setAlignment(Qt::AlignTop);
    // 创建曲线
    QLineSeries* series = new QLineSeries();
    series->setName(lineName);
    series->setColor(colors);
    int len=xDataList.size();
    for (int j = 0; j < len; ++j) {
        series->append(xDataList[j], yDataList[j]);
    }
    chart->addSeries(series);

    // 坐标轴配置
    QValueAxis* axisX = new QValueAxis();
    axisX->setRange(0, xDataList.last());  // 设置X轴范围
    axisX->setTitleText("Time (S)");
    axisX->setGridLineVisible(false);

    QValueAxis* axisY = new QValueAxis();
    axisY->setTitleText("");
    axisY->setGridLineColor(QColor(200, 200, 200, 100));
    axisY->setRange(0,120);
    axisY->setLabelsFont(QFont("微软雅黑", 8));

    chart->addAxis(axisX, Qt::AlignBottom);
    chart->addAxis(axisY, Qt::AlignLeft);
    // chart->legend()->show();
    chart->legend()->setVisible(true);

    // 关联所有系列到坐标轴
    series->attachAxis(axisX);
    series->attachAxis(axisY);

    chartView->setChart(chart);
    chartView->setRenderHint(QPainter::Antialiasing);
}
